package com.hwua.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hwua.entity.*;
import com.hwua.service.INewsService;
import com.hwua.service.IProductAllService;
import com.hwua.service.IProductService;
import com.hwua.service.Impl.NewsServiceImpl;
import com.hwua.service.Impl.ProductAllServiceImpl;
import com.hwua.service.Impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;

@WebServlet("/news")
public class NewsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        HttpSession session = request.getSession(false);
        INewsService newsService = new NewsServiceImpl();
        String param = request.getParameter("param");
        String id = request.getParameter("id");
        /*NEWS*/
        Map<String, Object> map = new HashMap<>();
        IProductService productService = new ProductServiceImpl();
        IProductAllService productAllService = new ProductAllServiceImpl();
        List<News> news = new ArrayList<>();
        try {
            news = newsService.showAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("tit", news);

        if (param.equals("showall")) {
            try {
                /* 此处为商品分类__总标题*/
                List<ProductCategory> productCategories = productService.queryList();
                request.setAttribute("product", productCategories);
            } catch (Exception e) {
                e.printStackTrace();
            }
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        } else if (param.equals("read")) {
            try {
                News newss = newsService.showNewsById(Long.parseLong(id));
                request.setAttribute("news", newss);
                request.getRequestDispatcher("/news_view.jsp").forward(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (param.equals("showtwo")) {
            List<ProductCategory> pro = null;
            long ids=0L;
            try {
                List<ProductCategory> productCategories = productService.query();
                map.put("one", productCategories);
                session.setAttribute("firstlist", productCategories);
                for (ProductCategory productCategory : productCategories) {
                    ids = productCategory.getId();
                    pro = productService.queryByID((int) ids);
                    map.put(Long.toString(ids), pro);
                }
                String s = JSON.toJSONString(map);
                response.getWriter().write(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (param.equals("hotpro")) {
            try {
                List<Product> products = productAllService.queryAll();
                String s = JSON.toJSONString(products);
                response.getWriter().write(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (param.equals("proList")) {
            /*分页*/
            response.setContentType("application/json;charset=utf-8");
            Integer pageNo = Integer.parseInt(request.getParameter("pageNo"));
            Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
            PageEntity pageEntity = new PageEntity();
            try {
                List<Product> ProList = productAllService.queryAllForLimit((pageNo - 1) * pageSize, pageSize);
                pageEntity.setList(ProList);
                Long totalRecords = productAllService.queryCount();//得到总记录数
                pageEntity.setTotalRecords(totalRecords);
                pageEntity.setPageSize(pageSize);//每页显示的记录数
                pageEntity.setPageNo(pageNo);//当前页
                String pageEntityJson = JSON.toJSONString(pageEntity, SerializerFeature.PrettyFormat);
                response.getWriter().write(pageEntityJson);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (param.equals("showList")) {
            List<Product> list = new ArrayList<>();
            //获取报错在cookie中的商品编号
            Cookie[] cookies = request.getCookies();
            boolean flag = false;//默认没有找到指定名字的cookie
            if (cookies != null && cookies.length > 0) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("histroyId")) {
                        flag = true;//找到
                        String value = cookie.getValue();
                        String[] ids = value.split("-");
                        for (String i : ids) {
                            try {
                                Product product = productAllService.queryByProdId(Integer.parseInt(i));
                                list.add(product);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        request.setAttribute("list2", list);
                        response.getWriter().write(JSON.toJSONString(list, SerializerFeature.PrettyFormat));
                        break;
                    }
                }
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
